package com.huiquan.icd.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.icd.service.IcdDiseaseExternalService;

@Controller
public class IcdDiseaseExternalController {

	@Autowired
	private IcdDiseaseExternalService icdDiseaseExternalService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/icd/disease_external_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("checkFlag", "0");
			pageParam.put("hospitalId", "tz");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		Integer start = RequestParamUtil.getIntegerDefaultDi(req, "start", 1);
		String codeSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "codeSearch");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		String checkFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "checkFlag");
		String hospitalId = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "hospitalId");

		return icdDiseaseExternalService.list(start, codeSearch, exactSearchFlag, countPerPageStr,checkFlag,hospitalId);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_external_init.do")
	public ReturnData initExternalData(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		ReturnData rtn = icdDiseaseExternalService.initExternalData();
		return rtn;
	}
	@ResponseBody
	@RequestMapping(value = "/icd/disease_external_init_disease.do")
	public ReturnData initExternalDisease(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		ReturnData rtn = icdDiseaseExternalService.initExternalDisease();
		return rtn;
	}
	@ResponseBody
	@RequestMapping(value = "/icd/check_external_disease.do")
	public ReturnData checkExternalDisease(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String disease = req.getParameter("disease").trim();
		ReturnData rtn = icdDiseaseExternalService.checkExternalDisease(id, disease);
		return rtn;
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_external_change_disable.do")
	public ReturnData changeDisable(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String disable = req.getParameter("disable");
		ReturnData rtn = icdDiseaseExternalService.changeDisable(id, disable);
		return rtn;
	}
	
	@ResponseBody
	@RequestMapping(value = "/icd/disease_external_submit.do")
	public ReturnData submit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String ids = req.getParameter("ids");
		User user = (User) req.getSession().getAttribute("user");

		try {
			icdDiseaseExternalService.submit(ids,user);
		} catch (Exception e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage());
			return ReturnUtil.fail(rc);
		}
		return ReturnUtil.success();
	}
	

	@ResponseBody
	@RequestMapping(value = "/icd/change_external_disease.do")
	public ReturnData changeExternalDisease(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String disease = req.getParameter("disease").trim();
		User user = (User) req.getSession().getAttribute("user");

		ReturnData rtn;
		try {
			rtn = icdDiseaseExternalService.changeExternalDisease(id, disease, user);
		} catch (Exception e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage());
			return ReturnUtil.fail(rc);
		}
		return rtn;
	}
	
	@ResponseBody
	@RequestMapping(value = "/icd/disease_external_change_category.do")
	public ReturnData changeExternalCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String category = req.getParameter("category").trim();
		User user = (User) req.getSession().getAttribute("user");

		ReturnData rtn;
		try {
			rtn = icdDiseaseExternalService.changeExternalCategory(id, category, user);
		} catch (Exception e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage());
			return ReturnUtil.fail(rc);
		}
		return rtn;
	}
	
	
	@RequestMapping(value = "/icd/external_disease_export.do")
	public void export(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		icdDiseaseExternalService.export( resp);
	}
	
	@ResponseBody
	@RequestMapping(value = "/icd/external_disease_import.do")
	public ReturnData synonymImport(HttpServletRequest req, @RequestParam(required = false) MultipartFile file)
			throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		ReturnData rtn;
		try {
			rtn = icdDiseaseExternalService.externalImport( file, user);
		} catch (Exception e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage());
			return ReturnUtil.fail(rc);
		}
		return rtn;
	}
}
